package com.example.mvvmrxjavaokhttpretrofit.net;



import com.example.mvvmrxjavaokhttpretrofit.BuildConfig;
import com.example.mvvmrxjavaokhttpretrofit.net.interceptors.logging.Level;
import com.example.mvvmrxjavaokhttpretrofit.net.interceptors.logging.LoggingInterceptor;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.internal.platform.Platform;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;


public final class RestCreator {


    /**
     * 构建OkHttp
     */
    private static final class OKHttpHolder {
        private static final int TIME_OUT = 15;
        private static final OkHttpClient.Builder BUILDER = new OkHttpClient.Builder();

        private static OkHttpClient.Builder addInterceptor() {
            return BUILDER;
        }

        private static final OkHttpClient OK_HTTP_CLIENT = addInterceptor()
                .retryOnConnectionFailure(false)
                .connectTimeout(TIME_OUT, TimeUnit.SECONDS)
                .readTimeout(TIME_OUT, TimeUnit.SECONDS)
                .writeTimeout(TIME_OUT, TimeUnit.SECONDS)
//                .addInterceptor(new CacheInterceptor(App.getInstance()))
                .addInterceptor(new LoggingInterceptor
                        .Builder()//构建者模式
                        .loggable(BuildConfig.DEBUG) //是否开启日志打印
                        .setLevel(Level.BASIC) //打印的等级
                        .log(Platform.INFO) // 打印类型
//                        .addHeader("Cache-Control", "max-age=640000")
//                        .addHeader("Accept", "application/json")
//                        .addHeader("Content-Type", "application/json")
//                        .addHeader("Connection", "close")
                        .request("Request") // request的Tag
                        .response("Response")// Response的Tag
//                        .addHeader("log-header", "I am the log request header.") // 添加打印头, 注意 key 和 value 都不能是中文
                        .build())
//                .connectionPool(new ConnectionPool(TIME_OUT, 1, TimeUnit.SECONDS))
                .build();
    }

    /**
     * 构建全局Retrofit客户端
     */
    private static final class RetrofitHolder {
        private static final Retrofit RETROFIT_CLIENT = new Retrofit.Builder()
                .baseUrl(RDURL.OFFICIAL_BASE_URL)
                .client(OKHttpHolder.OK_HTTP_CLIENT)
                .addConverterFactory(ScalarsConverterFactory.create())
//                .addConverterFactory(GsonConverterFactory.create()) //使用gson作为数据转换器
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) //使用RxJava作为回调适配器
                .build();
    }

    /**
     * Service接口
     */
    private static final class RestServiceHolder {
        private static final RestService REST_SERVICE =
                RetrofitHolder.RETROFIT_CLIENT.create(RestService.class);
    }

    public static RestService getRestService() {
        return RestServiceHolder.REST_SERVICE;
    }

}
